package com.banyuan;

import java.sql.*;
import java.util.Date;

public class JDBCUpdate {
   static String url = "jdbc:mysql://127.0.0.1:3306/mydb2?useUnicode=true&characterEncoding=UTF-8&userSSL=false&serverTimezone=GMT%2B8";
   static String name = "root";
   static String pw = "12345678";

    public static void main(String[] args) {
        Connection conn = null;
        PreparedStatement pstm = null;

        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
             conn = DriverManager.getConnection(url,name,pw);

             String updateSql = "insert into person(username,birthday,sex,password) values(?,?,?,?)";

             pstm = conn.prepareStatement(updateSql);
             pstm.setObject(1,"孔子");
            pstm.setObject(2,new Date());
            pstm.setObject(3,false);
            pstm.setObject(4,"1234");

            int i = pstm.executeUpdate();
            System.out.println("影响了" + i + "条数据");


        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            if (pstm != null) {
                try {
                    pstm.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        }
    }
}
